package com.zwj.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.R;
import com.zwj.common.entity.AuditAdministrator;
import com.zwj.user.entity.UserAndRole;
import com.zwj.user.entity.UserId;
import com.zwj.user.security.PasswordSecurity;
import com.zwj.user.service.AuditAdministratorService;
import com.zwj.user.mapper.AuditAdministratorMapper;
import com.zwj.user.service.UserAndRoleService;
import com.zwj.user.service.UserIdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 23088
* @description 针对表【audit_administrator(审核管理员)】的数据库操作Service实现
* @createDate 2022-10-20 23:27:58
*/
@Service
public class AuditAdministratorServiceImpl extends ServiceImpl<AuditAdministratorMapper, AuditAdministrator>
    implements AuditAdministratorService{

    @Resource
    private UserIdService userIdService;

    @Resource
    private UserAndRoleService userAndRoleService;


    @Override
    @Transactional
    public String register(AuditAdministrator auditAdministrator) {

        //得到唯一ID
        UserId userId = new UserId(200);
        userIdService.save(userId);
        auditAdministrator.setId(userId.getUserId());

        //MD5加密
        String password = auditAdministrator.getPassword();
        String md5Password = PasswordSecurity.Md5Encoding(password);
        auditAdministrator.setPassword(md5Password);

        auditAdministrator.setCreateTime(LocalDateTime.now());
        auditAdministrator.setUpdateTime(LocalDateTime.now());
        auditAdministrator.setCreatorId(Long.valueOf((String) StpUtil.getLoginId()));

        boolean save = this.save(auditAdministrator);

        //用户与角色绑定
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setUserId(auditAdministrator.getId());
        userAndRole.setRoleId(2L);
        userAndRole.setCreateTime(LocalDateTime.now());

        userAndRoleService.save(userAndRole);

        if (!save)
            return "注册失败!";

        return "注册成功!";
    }

    @Override
    public Page<AuditAdministrator> getAuditAdministrators(int page, int pageSize,
                                                           String phoneNumber, String username) {

        LambdaQueryWrapper<AuditAdministrator> auditAdministratorWrapper = new LambdaQueryWrapper<>();
        auditAdministratorWrapper.eq(StrUtil.isNotEmpty(phoneNumber), AuditAdministrator::getPhoneNumber, phoneNumber);
        auditAdministratorWrapper.eq(StrUtil.isNotEmpty(username), AuditAdministrator::getUsername, username);

        return this.page(new Page<AuditAdministrator>(page, pageSize), auditAdministratorWrapper);
    }
}




